package com.kaka.bus.engine.rechange;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.BankBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.PayBean;
import com.kaka.bus.bean.Single;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.result.ResultActivity;
import com.kaka.bus.manager.BottomDialogManager;
import com.kaka.bus.util.U;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * Created by OnionMac on 2018/7/11.
 */

public class RechangeActivity extends BaseActivity<RechangePresenter> implements RechangeContract.View {
    public static final String TYPE = "type"; // 等待放款

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar_rl)
    RelativeLayout mToolbarRl;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rechange_paymoney)
    TextView mRechangePaymoney;
    @BindView(R.id.pay_f)
    TextView mPayF;
    @BindView(R.id.rechange_name)
    TextView mRechangeName;
    @BindView(R.id.rechange_phone)
    TextView mRechangePhone;
    @BindView(R.id.rechange_bankname)
    TextView mRechangeBankname;
    @BindView(R.id.item_iv_info_you)
    ImageView mItemIvInfoYou;
    @BindView(R.id.rechange_selectbank)
    LinearLayout mRechangeSelectbank;
    @BindView(R.id.rechange_bankcard)
    EditText mRechangeBankcard;
    @BindView(R.id.rechange_yzm)
    EditText mRechangeYzm;
    @BindView(R.id.rechange_getcode)
    TextView mRechangeGetcode;
    @BindView(R.id.rechange_pay)
    AppCompatButton mRechangePay;
    private List<BankBean> mBankList;


    private ArrayList<String> mBankCodeList = new ArrayList<>();
    private Map<String, String> mBankMap = new HashMap<>();
    private BottomDialogManager mBankMananer;
    private PayBean mPayInfo;
    private int proType;
    private String mBankCode;
    private String mBankname;


    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("支付");
        mPayF.setText("咔咔钱包");
        mBankMananer = new BottomDialogManager(this);
    }

    @Override
    protected void initData() {
        mPresenter.getBankList();
        mPresenter.getPayInfo(getUserId(), getToken());
        proType = getIntent().getIntExtra(TYPE, 0);
    }

    @Override
    public void getBankListSuccess(HttpWrapper<List<BankBean>> wrapper) {
        if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            mBankList = wrapper.getData();
            for (BankBean bankBean : mBankList) {
                mBankCodeList.add(bankBean.getBankName());
                mBankMap.put(bankBean.getBankName(), bankBean.getBankCode());
            }
            mBankMananer.setData(mBankCodeList);
        }
    }

    @Override
    public void getPayInfoS(HttpWrapper<PayBean> wrapper) {
        mPayInfo = wrapper.getData();
        if (!TextUtils.isEmpty(mPayInfo.getBankName())) {
            mRechangeBankname.setText(mPayInfo.getBankName());
            mBankname = mPayInfo.getBankName();
        }

        if (!TextUtils.isEmpty(mPayInfo.getBankNum())) {
            mRechangeBankcard.setText(mPayInfo.getBankNum());
        }
        if (!TextUtils.isEmpty(mPayInfo.getBankCode())) {
            mBankCode = mPayInfo.getBankCode();
        }

        mRechangePaymoney.setText(mPayInfo.getAmount());
        mRechangeName.setText(mPayInfo.getName());
        mRechangePhone.setText(mPayInfo.getPhone());
    }

    @Override
    protected void initListener() {
        mRechangeSelectbank.setOnClickListener(v -> {
            if (mBankCodeList != null && mBankList.size() > 0) {
                mBankMananer.show();
            }
        });

        mBankMananer.setOkListener(select -> {
            mBankname = select;
            mBankCode = mBankMap.get(mBankname);
            mRechangeBankname.setText(mBankname);
        });

        mRechangePay.setOnClickListener(v -> {
            if (mPayInfo == null) {
                return;
            }

//            String bankName = getText(mRechangeBankname);
            String bankCard = getText(mRechangeBankcard);
            String yzm = getText(mRechangeYzm);

            if (TextUtils.isEmpty(mBankCode) || TextUtils.isEmpty(mBankname) || mBankname.equals("请选择银行卡")) {
                showMessage("请选择银行");
                return;
            }

            if (TextUtils.isEmpty(bankCard)) {
                showMessage("请填写银行卡号");
                return;
            }

            if (TextUtils.isEmpty(yzm)) {
                showMessage("请填写验证码");
                return;
            }

            Map<String, String> map = new HashMap<>();
            map.put(Constant.USERID, getUserId());
            map.put("borrNum", mPayInfo.getBorrNum());
            map.put("borrId", mPayInfo.getBorrId());
            map.put("optAmount", mPayInfo.getAmount());
            map.put("bankName", mBankname);
            map.put("bankCode", mBankCode);
            map.put("bankNum", bankCard);
            map.put("phone", mPayInfo.getPhone());
            map.put("validateCode", yzm);
            mPresenter.pay(map);
        });


        RxView.clicks(mRechangeGetcode)
                .throttleFirst(3, TimeUnit.SECONDS)
                .subscribe(v -> {
                    String bankCard = getText(mRechangeBankcard);
                    if (TextUtils.isEmpty(bankCard)) {
                        showMessage("请填写银行卡号");
                        return;
                    }

                    mPresenter.getPayCode(mPayInfo.getPhone(),
                            AppCenter.getTokenKey(),
                            Constant.SENDDYNAMIC, Constant.PLATFORM, bankCard);
                });

        mRechangeGetcode.setOnClickListener(v -> {

            String bankCard = getText(mRechangeBankcard);
            if (TextUtils.isEmpty(bankCard)) {
                showMessage("请填写银行卡号");
                return;
            }

            mPresenter.getPayCode(mPayInfo.getPhone(),
                    AppCenter.getTokenKey(),
                    Constant.SENDDYNAMIC, Constant.PLATFORM, bankCard);
        });
    }

    @Override
    public void sendYzmS(HttpWrapper<Single> wrapper) {
        time();
        showMessage(wrapper.getInfo());
    }

    @Override
    public void payS(HttpWrapper<String> wrapper) {
        //{"code":201,"info":"只支持借记卡","success":false}
        /**
         * 支付结果 TODO
         */
        if (Constant.SUCCESS_CODE.equals(wrapper.getCode())) {
            Intent intent = new Intent(this, ResultActivity.class);
            intent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.REPAY_SUCCESS);
            intent.putExtra(ResultActivity.PROJECT_TYPE, proType);
            startActivity(intent);
            finish();
        } else {
            showMessage(wrapper.getInfo());
            Intent intent = new Intent(this, ResultActivity.class);
            intent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.PAYFIAL);
            intent.putExtra(ResultActivity.PROJECT_TYPE, proType);
            startActivity(intent);
            finish();
        }


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rechange;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    public void time() {
        showMessage("成功发送验证码");
        mRechangeGetcode.setEnabled(false);
        //倒计时60秒
        //每秒赋值
        //提示错误信息
        addSubscription(U.countdownFlowable(59)            //倒计时60秒
                .subscribe(
                        time -> mRechangeGetcode.setText("重新发送" + "(" + time + ")"),    //每秒赋值
                        U::errorUtil,             //提示错误信息
                        () -> {
                            mRechangeGetcode.setEnabled(true);
                            mRechangeGetcode.setText(getString(R.string.reSendyzm));
                        }));
    }
}
